package com.tiantianyuegou.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.tiantianyuegou.R;
import com.tiantianyuegou.model.ColorOrSizeModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ivany on 2016/1/20.
 */
public class AddColorOrSizeGridAdapter extends BaseAdapter {

    private Context mContext;
    private List<ColorOrSizeModel> mData = new ArrayList<>();

    public interface IChangeListener {
        void deleteData(int position);

        void addModel(EditText editText);
    }

    private IChangeListener mListener;

    public void setChangeListenr(IChangeListener listenr) {
        mListener = listenr;
    }

    public AddColorOrSizeGridAdapter(Context context) {
        mContext = context;
    }

    public AddColorOrSizeGridAdapter(Context context, List<ColorOrSizeModel> datas) {
        mContext = context;
        mData.clear();
        mData.addAll(datas);
    }

    public void setData(List<ColorOrSizeModel> datas) {
        mData.clear();
        mData.addAll(datas);
    }

    public void clear() {
        mData.clear();
    }

    public void addData(ColorOrSizeModel model) {
        mData.add(model);
    }

    @Override
    public int getCount() {
        return mData.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.gird_item_add_size, parent, false);
            holder = new ViewHolder();
            holder.mLinear = (LinearLayout) convertView.findViewById(R.id.item_add_size_linear);
            holder.mEt_input = (EditText) convertView.findViewById(R.id.item_add_size_content);
            holder.mImg_del = (ImageView) convertView.findViewById(R.id.item_add_size_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //当到达最后时，添加'自定义'
        if (position == mData.size()) {
            holder.mEt_input.setHint("自定义");
            holder.mLinear.setBackground(mContext.getResources().getDrawable(R.drawable.background_unselector));
            holder.mImg_del.setVisibility(View.GONE);
            holder.mEt_input.setEnabled(true);
            holder.mEt_input.setHintTextColor(mContext.getResources().getColor(R.color.textcolor_gray));
        } else {
            ColorOrSizeModel model = mData.get(position);
            if (model.isSelect) {
                holder.mLinear.setBackground(mContext.getResources().getDrawable(R.drawable.background_selector));
                holder.mImg_del.setVisibility(View.VISIBLE);
                holder.mEt_input.setEnabled(false);
                holder.mEt_input.setHintTextColor(mContext.getResources().getColor(R.color.textcolor_red));
                holder.mImg_del.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.deleteData(position);
                    }
                });
            } else {
                holder.mLinear.setBackground(mContext.getResources().getDrawable(R.drawable.background_unselector));
                holder.mImg_del.setVisibility(View.GONE);
                holder.mEt_input.setEnabled(true);
                holder.mEt_input.setHintTextColor(mContext.getResources().getColor(R.color.textcolor_gray));
            }
            holder.mEt_input.setText("");
            holder.mEt_input.setHint(model.label);
        }


        if (mListener != null) {
            mListener.addModel(holder.mEt_input);
        }
        return convertView;
    }


    class ViewHolder {
        LinearLayout mLinear;
        EditText mEt_input;
        ImageView mImg_del;
    }
}
